<?php
/**
 * PictureModel handles the database interations for zenphoto
 *  
 * author: David Driscoll
 **/
class PhotosModel extends Model {
	
	function __construct() {
		parent::__construct();
		
		$this->zen = $this->load->database('zenphoto',TRUE);
	}
	
	function getZenPhotoHash($username) {
		$user_info = $this->UserModel->getUserByName($username);
		if(!$this->UserModel->isUserInGroup($user_info->userid,$this->UserModel->getGroupID('photos'))) {
			return FALSE;
		}
		
		$this->zen->select('password')->from('zp_administrators')->where('user',$username);
		$user = $this->zen->get();
		if(!is_object($user)) 
		{
			log_message('error','Zenphoto db access is not working');
			return FALSE;
		}
		if($user->num_rows() != 1)
		{
			$this->UserModel->unassignuser($this->UserModel->getGroupID('photos'),$user_info->userid);
			return FALSE;
		}
		else {
			$row = $user->row();
			return $row->password;
		}
	}
	
	function getUsers() {
		$users = $this->zen->get('zp_administrators');
		return $users;
	}
	
	function addUser($user_info,$rank)
	{
		$already_exists = $this->zen->get_where('zp_administrators', array('user'=>$user_info->username));
		if(!is_object($already_exists))
		{
			log_message('error','Zenphoto database is not working');
			return FALSE;
		}
		if($already_exists->num_rows() > 0)
			return TRUE;
			
		if($rank) $rights = 75102;
		else $rights = 94;
		
		$insert = array(
			'user' => $user_info->username,
			'password' => md5($user_info->username),
			'name' => $user_info->display,
			'email' => $user_info->email,
			'rights' => $rights
		);
		
		$this->zen->insert('zp_administrators',$insert);
	}
	
	function removeUser($username)
	{
		$this->zen->delete('zp_administrators',array('user'=>$username));
		log_message('info',$this->zen->last_query());
	}
}
?>
